package com.hanhai.material.utils.jiandaoyun;

import com.alibaba.fastjson.JSONObject;
import com.hanhai.material.device.entity.*;
import com.hanhai.material.device.service.*;
import com.hanhai.material.model.productdata1.*;
import com.hanhai.material.scheduling.entity.SysProductPeople;
import com.hanhai.material.scheduling.service.SysProductPeopleService;
import com.hanhai.material.wages.entity.FinUserRoster;
import com.hanhai.material.wages.service.FinUserRosterService;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;

import static com.hanhai.material.device.entity.table.SysBadRecordTableDef.SYS_BAD_RECORD;
import static com.hanhai.material.device.entity.table.SysProductRecordDetentionRecordTableDef.SYS_PRODUCT_RECORD_DETENTION_RECORD;
import static com.hanhai.material.device.entity.table.SysProductRecordDetentionTableDef.SYS_PRODUCT_RECORD_DETENTION;
import static com.hanhai.material.scheduling.entity.table.SysProductPeopleTableDef.SYS_PRODUCT_PEOPLE;
import static com.hanhai.material.utils.TimeUtils.fromDateToString;
import static com.hanhai.material.wages.entity.table.FinUserRosterTableDef.FIN_USER_ROSTER;
import static com.mybatisflex.core.query.QueryMethods.distinct;
import static com.mybatisflex.core.query.QueryMethods.sum;

/**
 * @author Administrator
 */
@Component
@Log4j2
public class PushToJianDaoYunUtil1 {

    @Value("${jiandaoyun.Authorization}")
    private String authorization;
    @Value("${jiandaoyun.url}")
    private String baseUrl;

    @Autowired
    SysProductRecordIntoService sysProductRecordIntoService;

    @Autowired
    SysProductRecordDetentionService sysProductRecordDetentionService;

    @Autowired
    SysBadRecordService sysBadRecordService;

    @Autowired
    SysProductPeopleService sysProductPeopleService;

    @Autowired
    SysPlcService sysPlcService;

    @Autowired
    SysProductRecordDetentionRecordService sysProductRecordDetentionRecordService;

    @Autowired
    FinUserRosterService finUserRosterService;


    public String pushToJianDaoYunSingle1(SysProductRecordDetentionRecord sysProductRecordDetentionRecord) {
        RestTemplate restTemplate = new RestTemplate();
        String url = baseUrl + "/v5/app/entry/data/create";
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
        headers.set("Authorization", authorization);
        ProductOtherTodayDataSingle productTodayDataSingle = this.pushProductTodayDataSingle1(sysProductRecordDetentionRecord);
        if(productTodayDataSingle!=null){
            productTodayDataSingle.setEntry_id("641553dd65484a0008f764dc");
            productTodayDataSingle.setApp_id("6381bdaf6af134000ad6629f");
            productTodayDataSingle.setOp("data_create");
            HttpEntity<ProductOtherTodayDataSingle> httpEntity = new HttpEntity<>(productTodayDataSingle, headers);
            ResponseEntity<Object> responseEntity = restTemplate.postForEntity(url, httpEntity, Object.class);
            log.error("ENtity:{}", JSONObject.toJSONString(httpEntity));
            JSONObject jsonObject = (JSONObject) JSONObject.toJSON(responseEntity.getBody());
            log.error("JSONOBJECT:{}", JSONObject.toJSONString(jsonObject));
            return jsonObject.getJSONObject("data").getString("_id");
        }else{
            return null;
        }



    }

    public String updateToJianDaoYunSingle1(SysProductRecordDetentionRecord sysProductRecordDetentionRecord) {
        RestTemplate restTemplate = new RestTemplate();
        String url = baseUrl + "/v5/app/entry/data/update";
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
        headers.set("Authorization", authorization);
        UpdateProductOtherTodayDataSingle updateProductTodayDataSingle = this.updteProductTodayDataSingle1(sysProductRecordDetentionRecord);
        log.error("Update:{}", JSONObject.toJSONString(updateProductTodayDataSingle));
        updateProductTodayDataSingle.setEntry_id("641553dd65484a0008f764dc");
        updateProductTodayDataSingle.setApp_id("6381bdaf6af134000ad6629f");
        updateProductTodayDataSingle.setOp("data_update");
        updateProductTodayDataSingle.setData_id(sysProductRecordDetentionRecord.getDataId());
        HttpEntity<UpdateProductOtherTodayDataSingle> httpEntity = new HttpEntity<>(updateProductTodayDataSingle, headers);
        ResponseEntity<Object> responseEntity = restTemplate.postForEntity(url, httpEntity, Object.class);
        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(responseEntity.getBody());
        return jsonObject.getJSONObject("data").getString("_id");


    }

    public ProductOtherTodayDataSingle pushProductTodayDataSingle1(SysProductRecordDetentionRecord sysProductRecordDetentionRecord) {

        log.error("入库数据：{}", JSONObject.toJSONString(sysProductRecordDetentionRecord));
        QueryWrapper queryWrapper = QueryWrapper.create();

        SysProductRecordDetention productRecordDetention = sysProductRecordDetentionRecord.getProductRecordDetention();
        if (productRecordDetention.getReason() != null) {
            SysProductRecordShift productRecordShift = sysProductRecordDetentionRecord.getProductRecordShift();
            SysProductInfo productInfo = sysProductRecordDetentionRecord.getProductInfo();
            Date productDate = sysProductRecordDetentionRecord.getProductDateInto();
            LocalDate now=productDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            ProductOtherTodayDataSingle productTodayDataSingle = new ProductOtherTodayDataSingle();
            queryWrapper=QueryWrapper.create();
            queryWrapper.where(FIN_USER_ROSTER.USER_NAME.eq(sysProductRecordDetentionRecord.getLineManager()));
            queryWrapper.where(FIN_USER_ROSTER.POSITION.eq("线长"));
            queryWrapper.where(FIN_USER_ROSTER.ARCHIVES_DATE.between(now.minusDays(5),now));
            queryWrapper.limit(1);
            FinUserRoster finUserRoster=this.finUserRosterService.getOne(queryWrapper);
            String process = productRecordDetention.getReason();
            if(process.contains("液氮")){
                process="液氮";
            }else if(process.contains("埋钉")){
                process="埋钉";
            }else if(process.contains("喷砂")){
                process="喷砂";
            }
            Data data=new Data();
            /*数据Key*/
            Key key = new Key();
            key.setValue(fromDateToString("yyyyMMdd", productDate) +  sysProductRecordDetentionRecord.getProductId() + finUserRoster.getShift()+process);
            data.setKey(key);

            Process_category process_category=new Process_category();
            process_category.setValue(process);
            data.setProcess_category(process_category);

            Is_entry is_entry=new Is_entry();
            is_entry.setValue("是");
            data.setIs_entry(is_entry);




            /*员工姓名*/
            User_name user_name = new User_name();
            user_name.setValue(finUserRoster.getUserId());
            data.setUser_name(user_name);


            Product_id product_id = new Product_id();
            product_id.setValue(sysProductRecordDetentionRecord.getProductId());
            data.setProduct_id(product_id);



            /*生产日期*/
            Product_date product_date = new Product_date();
            product_date.setValue(productDate);
            data.setProduct_date(product_date);

            /*开始时间*/
            Product_start_time product_start_time = new Product_start_time();
            LocalDateTime startTime=sysProductRecordDetentionRecord.getShift().equals("白班")?now.atTime(8,0,0):now.atTime(20,0,0);
            product_start_time.setValue(Date.from(startTime.atZone(ZoneId.systemDefault()).toInstant()));
            data.setProduct_start_time(product_start_time);

            /*结束时间*/
            Product_end_time product_end_time = new Product_end_time();
            LocalDateTime endTime=sysProductRecordDetentionRecord.getShift().equals("白班")?now.atTime(20,0,0):now.atTime(8,0,0);
            product_end_time.setValue(Date.from(endTime.atZone(ZoneId.systemDefault()).toInstant()));
            data.setProduct_end_time(product_end_time);

            long times = endTime.getMinute() - startTime.getMinute();
            double hours = times /60;
            BigDecimal bigDecimal = BigDecimal.valueOf(hours);
            double hour = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            if (hour >= 5) {
                hour = hour - 1;
            }
            hour = hour == 0 ? 11 : hour;
            Actual_work_time_num actual_work_time_num = new Actual_work_time_num();
            actual_work_time_num.setValue(hour);
            data.setActual_work_time_num(actual_work_time_num);

            /*班别*/
            Shift shift = new Shift();
            shift.setValue(finUserRoster.getShift());
            data.setShift(shift);

            Is_auto is_auto = new Is_auto();
            is_auto.setValue("全自动");
            data.setIs_auto(is_auto);
            log.error("产品");


            if (productInfo != null) {
                Customer_name customer_name = new Customer_name();
                customer_name.setValue(productInfo.getCustomerName());
                data.setCustomer_name(customer_name);

                Standard_recycle standard_recycle = new Standard_recycle();
                standard_recycle.setValue(productInfo.getStandardCycle().intValue());
                data.setStandard_recycle(standard_recycle);

                Machine_type_name machine_type_name = new Machine_type_name();
                machine_type_name.setValue(productInfo.getMachineType());
                data.setMachine_type_name(machine_type_name);

                log.error("机种");
                Machine_type_short_name machine_type_short_name = new Machine_type_short_name();
                machine_type_short_name.setValue(productInfo.getMachineType() + "-" + productInfo.getMachineItem());
                data.setMachine_type_short_name(machine_type_short_name);
                log.error("standard_num");
                int standardProductNum = (int) (80*hour);
                Standard_num standard_num = new Standard_num();
                standard_num.setValue(standardProductNum);
                data.setStandard_num(standard_num);
                log.error("input_quantity");
                Input_quantity input_quantity = new Input_quantity();
                if (sysProductRecordDetentionRecord.getNum() != null) {
                    input_quantity.setValue(sysProductRecordDetentionRecord.getNum());
                }

                data.setInput_quantity(input_quantity);

                log.error("标准周期");
                double rate = 0;
                double rateN = 0;

                if (standardProductNum > 0) {
                    rate = (double) (sysProductRecordDetentionRecord.getNum()) / standardProductNum;
                    BigDecimal rateBigDecimal = new BigDecimal(rate);
                    rateN = rateBigDecimal.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                }

                log.error("辅助信息");
                Aux_achievement_rate aux_achievement_rate = new Aux_achievement_rate();
                aux_achievement_rate.setValue(rateN);
                data.setAux_achievement_rate(aux_achievement_rate);

            }

            Machine_type_product_name machine_type_product_name=new Machine_type_product_name();
            machine_type_product_name.setValue(sysProductRecordDetentionRecord.getProductName());
            data.setMachine_type_product_name(machine_type_product_name);

            Machine_type_product_id machine_type_product_id=new Machine_type_product_id();
            machine_type_product_id.setValue(sysProductRecordDetentionRecord.getProductId());
            data.setMachine_type_product_id(machine_type_product_id);


            Good_num good_num = new Good_num();

            good_num.setValue(sysProductRecordDetentionRecord.getGoodNumPeople());


            data.setGood_num(good_num);


            log.error("辅助信息");
            Aux_bad_rate aux_bad_rate = new Aux_bad_rate();
            double rateN = 0;
            if (sysProductRecordDetentionRecord.getNum() != null) {
                if (sysProductRecordDetentionRecord.getNum() > 0) {
                    double rate = (double) sysProductRecordDetentionRecord.getBadNumPeople() / sysProductRecordDetentionRecord.getNum();
                    BigDecimal rateBigDecimal = new BigDecimal(rate);
                    rateN = rateBigDecimal.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                }
            }

            aux_bad_rate.setValue(rateN);
            data.setAux_bad_rate(aux_bad_rate);

            Bad_rate bad_rate=new Bad_rate();
            bad_rate.setValue(rateN);
            data.setBad_rate(bad_rate);

            Good_rate good_rate=new Good_rate();
            good_rate.setValue(1-rateN);
            data.setGood_rate(good_rate);
            Dept dept=new Dept();
            dept.setValue("成型车间");
            data.setDept(dept);



            Aux_shift aux_shift = new Aux_shift();
            aux_shift.setValue(finUserRoster.getShift());
            data.setAux_shift(aux_shift);


            Aux_product_name aux_product_name = new Aux_product_name();
            aux_product_name.setValue(sysProductRecordDetentionRecord.getProductName());
            data.setAux_product_name(aux_product_name);

            Today_into_num today_into_num = new Today_into_num();

            today_into_num.setValue(sysProductRecordDetentionRecord.getGoodNumPeople());


            data.setToday_into_num(today_into_num);

            queryWrapper = QueryWrapper.create();
//        queryWrapper.where(SYS_PRODUCT_PEOPLE.WORK_TIME.ge(fromDateToString("yyyy-MM-dd HH:00:00",productRecordInto.getActualStartTime())));
//        queryWrapper.where(SYS_PRODUCT_PEOPLE.WORK_TIME.lt(productRecordInto.getActualEndTime()));
//        queryWrapper.where(SYS_PRODUCT_PEOPLE.MACHINE_NAME.eq(productRecordShift.getMachineName()));
//        queryWrapper.where(SYS_PRODUCT_PEOPLE.WORK_PLAN_ID.eq(productRecordInto.getWorkPlanId()));
            queryWrapper.where(SYS_PRODUCT_PEOPLE.MACHINE_NAME.eq(process));
            queryWrapper.where(SYS_PRODUCT_PEOPLE.SHIFT.eq(sysProductRecordDetentionRecord.getShift()));
            queryWrapper.where(SYS_PRODUCT_PEOPLE.WORK_DATE.eq(fromDateToString("yyyy-MM-dd",sysProductRecordDetentionRecord.getProductDateInto())));
            queryWrapper.select(distinct(SYS_PRODUCT_PEOPLE.JOB_NO), SYS_PRODUCT_PEOPLE.USER_NAME);
            log.error("员工：{}", queryWrapper.toSQL());
            List<SysProductPeople> productPeopleList = this.sysProductPeopleService.list(queryWrapper);
            log.error("ProductPeopleList:{}", JSONObject.toJSONString(productPeopleList));
            int i = 0;
            People people = new People();
            List<PeopleInfo> peopleInfoList = new ArrayList<>();
            List<String> nameList = new ArrayList<>();
            StringJoiner userNameList = new StringJoiner(",");
            StringJoiner jobNoList = new StringJoiner(",");
            for (SysProductPeople productPeople : productPeopleList) {
                nameList.add(productPeople.getUserName());
                PeopleInfo peopleInfo = new PeopleInfo();
                i++;
                People_index people_index = new People_index();
                people_index.setValue(i);
                peopleInfo.setPeople_index(people_index);
                userNameList.add(productPeople.getUserName());
                jobNoList.add(productPeople.getJobNo());
                People_job_no people_job_no = new People_job_no();
                people_job_no.setValue(productPeople.getJobNo());
                peopleInfo.setPeople_job_no(people_job_no);

                People_user_name people_user_name = new People_user_name();
                people_user_name.setValue(productPeople.getUserName());
                peopleInfo.setPeople_user_name(people_user_name);
                peopleInfoList.add(peopleInfo);

            }
            people.setValue(peopleInfoList);
            data.setPeople(people);

            User_name1 user_name1 = new User_name1();
            user_name1.setValue(nameList);
            data.setUser_name1(user_name1);


            Remark_name remark_name = new Remark_name();
            remark_name.setValue(userNameList.toString());
            data.setRemark_name(remark_name);
            int peopleSize = productPeopleList.size();
            Aux_people_num aux_people_num = new Aux_people_num();
            aux_people_num.setValue(peopleSize);
            data.setAux_people_num(aux_people_num);

            People_num people_num = new People_num();
            people_num.setValue(String.valueOf(peopleSize));
            data.setPeople_num(people_num);
            People_num1 people_num1=new People_num1();
            people_num1.setValue(peopleSize);
            data.setPeople_num1(people_num1);

            Aux_product_time aux_product_time = new Aux_product_time();
            aux_product_time.setValue(productDate.getTime());
            data.setAux_product_time(aux_product_time);

            Line_manager_job_no line_manager_job_no = new Line_manager_job_no();
            line_manager_job_no.setValue(finUserRoster.getJobNo());
            data.setLine_manager_job_no(line_manager_job_no);

            Line_manager_name line_manager_name = new Line_manager_name();
            line_manager_name.setValue(finUserRoster.getUserName());
            data.setLine_manager_name(line_manager_name);


            Creator creator = new Creator();
            creator.setValue(finUserRoster.getUserId());
            data.setCreator(creator);
            productTodayDataSingle.setData(data);
            return productTodayDataSingle;
        }
        return null;
    }

    public UpdateProductOtherTodayDataSingle updteProductTodayDataSingle1(SysProductRecordDetentionRecord sysProductRecordDetentionRecord) {

        log.error("入库数据：{}", JSONObject.toJSONString(sysProductRecordDetentionRecord));
        QueryWrapper queryWrapper = QueryWrapper.create();

        SysProductRecordDetention productRecordDetention = sysProductRecordDetentionRecord.getProductRecordDetention();
        if (productRecordDetention.getReason() != null) {
            SysProductRecordShift productRecordShift = sysProductRecordDetentionRecord.getProductRecordShift();
            SysProductInfo productInfo = sysProductRecordDetentionRecord.getProductInfo();
            Date productDate = sysProductRecordDetentionRecord.getProductDateInto();
            LocalDate now=productDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            UpdateProductOtherTodayDataSingle productTodayDataSingle = new UpdateProductOtherTodayDataSingle();
            queryWrapper=QueryWrapper.create();
            queryWrapper.where(FIN_USER_ROSTER.USER_NAME.eq(sysProductRecordDetentionRecord.getLineManager()));
            queryWrapper.where(FIN_USER_ROSTER.POSITION.eq("线长"));
            queryWrapper.where(FIN_USER_ROSTER.ARCHIVES_DATE.between(now.minusDays(5),now));
            queryWrapper.limit(1);
            FinUserRoster finUserRoster=this.finUserRosterService.getOne(queryWrapper);
            String process = productRecordDetention.getReason();
            if(process.contains("液氮")){
                process="液氮";
            }else if(process.contains("埋钉")){
                process="埋钉";
            }else if(process.contains("喷砂")){
                process="喷砂";
            }
            Data data=new Data();
            /*数据Key*/
            Key key = new Key();
            key.setValue(fromDateToString("yyyyMMdd", productDate) +  sysProductRecordDetentionRecord.getProductId() + finUserRoster.getShift()+process);
            data.setKey(key);

            Process_category process_category=new Process_category();
            process_category.setValue(process);
            data.setProcess_category(process_category);

            Is_entry is_entry=new Is_entry();
            is_entry.setValue("是");
            data.setIs_entry(is_entry);




            /*员工姓名*/
            User_name user_name = new User_name();
            user_name.setValue(finUserRoster.getUserId());
            data.setUser_name(user_name);


            Product_id product_id = new Product_id();
            product_id.setValue(sysProductRecordDetentionRecord.getProductId());
            data.setProduct_id(product_id);



            /*生产日期*/
            Product_date product_date = new Product_date();
            product_date.setValue(productDate);
            data.setProduct_date(product_date);

            /*开始时间*/
            Product_start_time product_start_time = new Product_start_time();
            LocalDateTime startTime=sysProductRecordDetentionRecord.getShift().equals("白班")?now.atTime(8,0,0):now.atTime(20,0,0);
            product_start_time.setValue(Date.from(startTime.atZone(ZoneId.systemDefault()).toInstant()));
            data.setProduct_start_time(product_start_time);

            /*结束时间*/
            Product_end_time product_end_time = new Product_end_time();
            LocalDateTime endTime=sysProductRecordDetentionRecord.getShift().equals("白班")?now.atTime(20,0,0):now.atTime(8,0,0);
            product_end_time.setValue(Date.from(endTime.atZone(ZoneId.systemDefault()).toInstant()));
            data.setProduct_end_time(product_end_time);

            long times = endTime.getMinute() - startTime.getMinute();
            double hours = times /60;
            BigDecimal bigDecimal = BigDecimal.valueOf(hours);
            double hour = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            if (hour >= 5) {
                hour = hour - 1;
            }
            hour = hour == 0 ? 11 : hour;
            Actual_work_time_num actual_work_time_num = new Actual_work_time_num();
            actual_work_time_num.setValue(hour);
            data.setActual_work_time_num(actual_work_time_num);

            /*班别*/
            Shift shift = new Shift();
            shift.setValue(finUserRoster.getShift());
            data.setShift(shift);

            Is_auto is_auto = new Is_auto();
            is_auto.setValue("全自动");
            data.setIs_auto(is_auto);
            log.error("产品");


            if (productInfo != null) {
                Customer_name customer_name = new Customer_name();
                customer_name.setValue(productInfo.getCustomerName());
                data.setCustomer_name(customer_name);

                Standard_recycle standard_recycle = new Standard_recycle();
                standard_recycle.setValue(productInfo.getStandardCycle().intValue());
                data.setStandard_recycle(standard_recycle);

                Machine_type_name machine_type_name = new Machine_type_name();
                machine_type_name.setValue(productInfo.getMachineType());
                data.setMachine_type_name(machine_type_name);

                log.error("机种");
                Machine_type_short_name machine_type_short_name = new Machine_type_short_name();
                machine_type_short_name.setValue(productInfo.getMachineType() + "-" + productInfo.getMachineItem());
                data.setMachine_type_short_name(machine_type_short_name);
                log.error("standard_num");
                int standardProductNum = (int) (80*hour);
                Standard_num standard_num = new Standard_num();
                standard_num.setValue(standardProductNum);
                data.setStandard_num(standard_num);
                log.error("input_quantity");
                Input_quantity input_quantity = new Input_quantity();
                if (sysProductRecordDetentionRecord.getNum() != null) {
                    input_quantity.setValue(sysProductRecordDetentionRecord.getNum());
                }

                data.setInput_quantity(input_quantity);

                log.error("标准周期");
                double rate = 0;
                double rateN = 0;

                if (standardProductNum > 0) {
                    rate = (double) (sysProductRecordDetentionRecord.getNum()) / standardProductNum;
                    BigDecimal rateBigDecimal = new BigDecimal(rate);
                    rateN = rateBigDecimal.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                }

                log.error("辅助信息");
                Aux_achievement_rate aux_achievement_rate = new Aux_achievement_rate();
                aux_achievement_rate.setValue(rateN);
                data.setAux_achievement_rate(aux_achievement_rate);

            }

            Machine_type_product_name machine_type_product_name=new Machine_type_product_name();
            machine_type_product_name.setValue(sysProductRecordDetentionRecord.getProductName());
            data.setMachine_type_product_name(machine_type_product_name);

            Machine_type_product_id machine_type_product_id=new Machine_type_product_id();
            machine_type_product_id.setValue(sysProductRecordDetentionRecord.getProductId());
            data.setMachine_type_product_id(machine_type_product_id);


            Good_num good_num = new Good_num();

            good_num.setValue(sysProductRecordDetentionRecord.getGoodNumPeople());


            data.setGood_num(good_num);


            log.error("辅助信息");
            Aux_bad_rate aux_bad_rate = new Aux_bad_rate();
            double rateN = 0;
            if (sysProductRecordDetentionRecord.getNum() != null) {
                if (sysProductRecordDetentionRecord.getNum() > 0) {
                    double rate = (double) sysProductRecordDetentionRecord.getBadNumPeople() / sysProductRecordDetentionRecord.getNum();
                    BigDecimal rateBigDecimal = new BigDecimal(rate);
                    rateN = rateBigDecimal.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                }
            }

            aux_bad_rate.setValue(rateN);
            data.setAux_bad_rate(aux_bad_rate);

            Bad_rate bad_rate=new Bad_rate();
            bad_rate.setValue(rateN);
            data.setBad_rate(bad_rate);

            Good_rate good_rate=new Good_rate();
            good_rate.setValue(1-rateN);
            data.setGood_rate(good_rate);
            Dept dept=new Dept();
            dept.setValue("成型车间");
            data.setDept(dept);



            Aux_shift aux_shift = new Aux_shift();
            aux_shift.setValue(finUserRoster.getShift());
            data.setAux_shift(aux_shift);


            Aux_product_name aux_product_name = new Aux_product_name();
            aux_product_name.setValue(sysProductRecordDetentionRecord.getProductName());
            data.setAux_product_name(aux_product_name);

            Today_into_num today_into_num = new Today_into_num();

            today_into_num.setValue(sysProductRecordDetentionRecord.getGoodNumPeople());


            data.setToday_into_num(today_into_num);

            queryWrapper = QueryWrapper.create();
//        queryWrapper.where(SYS_PRODUCT_PEOPLE.WORK_TIME.ge(fromDateToString("yyyy-MM-dd HH:00:00",productRecordInto.getActualStartTime())));
//        queryWrapper.where(SYS_PRODUCT_PEOPLE.WORK_TIME.lt(productRecordInto.getActualEndTime()));
//        queryWrapper.where(SYS_PRODUCT_PEOPLE.MACHINE_NAME.eq(productRecordShift.getMachineName()));
//        queryWrapper.where(SYS_PRODUCT_PEOPLE.WORK_PLAN_ID.eq(productRecordInto.getWorkPlanId()));
            queryWrapper.where(SYS_PRODUCT_PEOPLE.MACHINE_NAME.eq(process));
            queryWrapper.where(SYS_PRODUCT_PEOPLE.SHIFT.eq(sysProductRecordDetentionRecord.getShift()));
            queryWrapper.where(SYS_PRODUCT_PEOPLE.WORK_DATE.eq(fromDateToString("yyyy-MM-dd",sysProductRecordDetentionRecord.getProductDateInto())));
            queryWrapper.select(distinct(SYS_PRODUCT_PEOPLE.JOB_NO), SYS_PRODUCT_PEOPLE.USER_NAME);
            log.error("员工：{}", queryWrapper.toSQL());
            List<SysProductPeople> productPeopleList = this.sysProductPeopleService.list(queryWrapper);
            log.error("ProductPeopleList:{}", JSONObject.toJSONString(productPeopleList));
            int i = 0;
            People people = new People();
            List<PeopleInfo> peopleInfoList = new ArrayList<>();
            List<String> nameList = new ArrayList<>();
            StringJoiner userNameList = new StringJoiner(",");
            StringJoiner jobNoList = new StringJoiner(",");
            for (SysProductPeople productPeople : productPeopleList) {
                nameList.add(productPeople.getUserName());
                PeopleInfo peopleInfo = new PeopleInfo();
                i++;
                People_index people_index = new People_index();
                people_index.setValue(i);
                peopleInfo.setPeople_index(people_index);
                userNameList.add(productPeople.getUserName());
                jobNoList.add(productPeople.getJobNo());
                People_job_no people_job_no = new People_job_no();
                people_job_no.setValue(productPeople.getJobNo());
                peopleInfo.setPeople_job_no(people_job_no);

                People_user_name people_user_name = new People_user_name();
                people_user_name.setValue(productPeople.getUserName());
                peopleInfo.setPeople_user_name(people_user_name);
                peopleInfoList.add(peopleInfo);

            }
            people.setValue(peopleInfoList);
            data.setPeople(people);

            User_name1 user_name1 = new User_name1();
            user_name1.setValue(nameList);
            data.setUser_name1(user_name1);


            Remark_name remark_name = new Remark_name();
            remark_name.setValue(userNameList.toString());
            data.setRemark_name(remark_name);
            int peopleSize = productPeopleList.size();
            Aux_people_num aux_people_num = new Aux_people_num();
            aux_people_num.setValue(peopleSize);
            data.setAux_people_num(aux_people_num);

            People_num people_num = new People_num();
            people_num.setValue(String.valueOf(peopleSize));
            data.setPeople_num(people_num);
            People_num1 people_num1=new People_num1();
            people_num1.setValue(peopleSize);
            data.setPeople_num1(people_num1);

            Aux_product_time aux_product_time = new Aux_product_time();
            aux_product_time.setValue(productDate.getTime());
            data.setAux_product_time(aux_product_time);

            Line_manager_job_no line_manager_job_no = new Line_manager_job_no();
            line_manager_job_no.setValue(finUserRoster.getJobNo());
            data.setLine_manager_job_no(line_manager_job_no);

            Line_manager_name line_manager_name = new Line_manager_name();
            line_manager_name.setValue(finUserRoster.getUserName());
            data.setLine_manager_name(line_manager_name);


            Creator creator = new Creator();
            creator.setValue(finUserRoster.getUserId());
            data.setCreator(creator);
            productTodayDataSingle.setData(data);
            return productTodayDataSingle;
        }
        return null;
    }

}
